<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<head>
    <script language="javascript">
        function check_validationlogin()
        {
            var error = "";
            if(frmLogin.txtUserName.value == "")
                error+= "Username can't not empty!\n";
            if(frmLogin.txtPassword.value == "")
                error+= "Password can't not empty!";
            
            if(error !="")
            {
                alert(error);
                return false;
            }
            //window.location = "login.php";
          
        }
        
        function showUsers(id, type)
        {
            xmlHttp = CreateXMLHttpRequest();
            var serverURL = "http://localhost/exercise_3_php_mysql_javascript/listUsers.php?id=" + id + "&type=" +type +  "&t=" + (new Date()).getTime();
            window.open(serverURL, "List user", "status = 1, height = 200, width = 200, resizable = 0");
        }
    </script>
</head>
<body>
    <?PHP
    require 'connect.inc';
    require 'session.inc';
    $res = "";
    $state = 0;
    $result;

    if ($_SESSION["isLogin"] == 1)
        $state = 1;
    if (isset($_REQUEST["btnLogin"])) {
        $username = $_REQUEST["txtUserName"];
        $pass = md5($_REQUEST["txtPassword"]);
        $strSQL = "SELECT * FROM USERS WHERE user_name = '$username' AND password = '$pass'";
        $result = mysql_query($strSQL);
        if ($result && mysql_num_rows($result) > 0) {
            $state = 1;
            $_SESSION["isLogin"] = 1;
            $_SESSION["username"] = $username;
            while ($row = mysql_fetch_array($result)) {
                $_SESSION["fullName"] = $row["full_name"];
                $_SESSION["email"] = $row["email"];
            }
        }
    }
    if ($state == 1) {
        $res = "Wellcom <a href='#'>" . $_SESSION["username"] . "</a>!
                |
                <a href='logout.php'>Logout</a>
                <hr>";

        $res .= " <p> List department:</p>";
        $strSQL = "SELECT d.department_id, d.department_name,
                            (SELECT COUNT(user_id) 
                            FROM users us
                            WHERE us.department_id = d.department_id) as SumUser
                    FROM departments d";
        $result = mysql_query($strSQL);
        if (!$result)
            $res .= "Department is empty!";
        else {
            $res .= "<table class='f11' border='1'>
                    <tr>
                        <th>STT</th>
                        <th>Department  ID</th>
                        <th>Department  name</th>
                        <th>Number  of  users</th> 
                       </tr>";
            $stt = 0;
            while ($row = mysql_fetch_array($result)) {
                $stt++;
                $id = $row["department_id"];
                $name = $row["department_name"];
                $sum = $row["SumUser"];
                $res .= "<tr>
                    <td>$stt</td>
                    <td>$id</td>
                    <td>$name</td>
                    <td><a href='#' onclick='showUsers($id,2)'>$sum</a></td>
               </tr>";
            }
            $res .= "</table>";
            $res .= " <p> List department:</p>";
        }
        $strSQL = "SELECT c.country_id, c.country_name,
                            (SELECT COUNT(user_id) 
                            FROM users us
                            WHERE us.country_id = c.country_id) as SumUser
                    FROM countries c";
        $result = mysql_query($strSQL);
        if (!$result)
            $res .= "Department is empty!";
        else {
            $res .= "<table class='f11' border='1'>";
            $res .= "<tr>
                        <th>STT</th>
                        <th>Country  ID</th>
                        <th>Country  name</th>
                        <th>Number  of  users</th> 
                       </tr>";
            $stt = 0;
            while ($row = mysql_fetch_array($result)) {
                $stt++;
                $id = $row["country_id"];
                $name = $row["country_name"];
                $sum = $row["SumUser"];
                $res .= "<tr>
                    <td>$stt</td>
                    <td>$id</td>
                    <td>$name</td>
                    <td><a href='#' onclick='showUsers($id,1)'>$sum</a></td>
               </tr>";
            }
            $res .= "</table>";
        }
    } else {

        $res .= "<a href='register.php'>Register</a>
    |
    <a href='login.php'>Login</a>
    <hr>
    <form onsubmit='return check_validationlogin();' action='login.php' method = 'POST' name='frmLogin'>
        User name:
        <input type='text' name='txtUserName'>
        <br>
        Password:
        <input type='password' name='txtPassword'>
        <br>
        <input type='submit' name='btnLogin' value='Login'>
    </form>";
    }
    echo $res;
    ?>


    <hr>
    <p>
        :
    </p>

</body>

